#include "BulletManager.h"
#include "ArrowBullet.h"
#include "AxeBullet.h"
#include "ShellBullet.h"

BulletManager::~BulletManager()
{
    for (Bullet *bullet: bulletList)
        delete bullet;
}

void BulletManager::OnUpdate(double delta)
{
    for (Bullet *bullet: bulletList)
        bullet->OnUpdate(delta);

    bulletList.erase(std::remove_if(
            bulletList.begin(), bulletList.end(),
            [](const Bullet *bullet) {
                bool deletable = bullet->CanRemove();
                if (deletable) delete bullet;
                return deletable;
            }), bulletList.end());
}

void BulletManager::OnRender(SDL_Renderer *renderer)
{
    for (Bullet *bullet: bulletList)
        bullet->OnRender(renderer);
}

std::vector<Bullet *> &BulletManager::GetBulletList()
{
    return bulletList;
}

void BulletManager::FireBullet(BulletType type, const Vector2 &position,
                               const Vector2 &velocity, double damage)
{
    Bullet *bullet = nullptr;

    switch (type) {
        case Arrow:
            bullet = new ArrowBullet();
            break;
        case Axe:
            bullet = new AxeBullet();
            break;
        case Shell:
            bullet = new ShellBullet();
            break;
        default:
            bullet = new ArrowBullet();
            break;
    }

    bullet->SetPosition(position);
    bullet->SetVelocity(velocity);
    bullet->SetDamage(damage);
    bulletList.push_back(bullet);
}